bluetooth-serial-port
Rust library for interacting with the Bluetooth stack via RFCOMM channels.
This library currently only works on Linux/BlueZ. You can find it on
crates.io.
Cargo.toml:
[dependencies]
bluetooth-serial-port = "0.2"
Important functions:
bluetooth_serial_port::scan_devices()
BtSocket::new()
BtSocket::connect()
BtSocket::read()
BtSocket::write()
Full example:
extern crate bluetooth_serial_port;
extern crate mio;
use bluetooth_serial_port::{BtProtocol, BtSocket};
use std::io::{Read, Write};
use mio::{EventLoop, Handler, PollOpt, Token, EventSet};
fn main() {
let devices = bluetooth_serial_port::scan_devices().unwrap();
if devices.len() == 0 { panic!("No devices found"); }
let device = &devices[0];
println!("Connecting to `{}` ({})", device.name, device.addr.to_string());
let mut socket = BtSocket::new(BtProtocol::RFCOMM).unwrap();
socket.connect(device.addr).unwrap();
let mut buffer = [0; 10];
let num_bytes_read = socket.read(&mut buffer[..]).unwrap();
let num_bytes_written = socket.write(&buffer[0..num_bytes_read]).unwrap();
println!("Read `{}` bytes, wrote `{}` bytes", num_bytes_read, num_bytes_written);
let mut event_loop = EventLoop::new().unwrap();
event_loop.register(&socket, Token(0), EventSet::readable() | EventSet::writable(),
PollOpt::edge() | PollOpt::oneshot()).unwrap();
struct NoopHandler;
impl Handler for NoopHandler { type Timeout = (); type Message = (); }
event_loop.run(&mut NoopHandler).unwrap();
}